# copy the log to a text file
sink("./tableA3_log.txt")

library(dplyr)
library(readxl)

dat <- read.csv("lucid_data.csv")

# Pull in factor scores

scores <- read_excel("factor_scores.xlsx") %>% 
  dplyr::select(`...2`, `...3`) %>% 
  dplyr::rename("variable"=`...2`,
         "factor_score"=`...3`)

# Create various statistics related to the issue items ####

## Percent choosing the neither support nor oppose column ####
ambivalence <- dat %>%
  mutate_at(vars(affordable_housing:voterid), .funs = ~ {
    case_when(
      .x == 1 ~ 0,
      .x == 2 ~ 0,
      .x == 3 ~ 1,
      .x == 4 ~ 0,
      .x == 5 ~ 0,
    )
  }) %>%
  summarise(across(affordable_housing:voterid, ~mean(., na.rm=T))) %>%
  rename_with(tolower) %>%
  pivot_longer(everything(), names_to = "variable", values_to = "pct_ambivalent")

## Net support among Democrats and Republicans ####

republicans <- filter(dat, political_party==8 | political_party==9 | political_party==10 | political_party==5)  %>%
  mutate_at(vars(affordable_housing:voterid), .funs = ~ {
    case_when(
      .x == 1 ~ 1,
      .x == 2 ~ 1,
      .x == 3 ~ 0,
      .x == 4 ~ -1,
      .x == 5 ~ -1,
    )
  }) %>%
  summarise(across(affordable_housing:voterid, ~mean(., na.rm=T))) %>%
  rename_with(tolower) %>%
  pivot_longer(everything(), names_to = "variable", values_to = "rep_support")

democrats <- filter(dat, political_party<4 | political_party==6)  %>%
  mutate_at(vars(affordable_housing:voterid), .funs = ~ {
    case_when(
      .x == 1 ~ 1,
      .x == 2 ~ 1,
      .x == 3 ~ 0,
      .x == 4 ~ -1,
      .x == 5 ~ -1,
    )
  }) %>%
  summarise(across(affordable_housing:voterid, ~mean(., na.rm=T))) %>%
  rename_with(tolower) %>%
  pivot_longer(everything(), names_to = "variable", values_to = "dem_support")

# Merge the files ####

dat2 <- merge(scores, ambivalence, by="variable")
dat2 <- merge(dat2, democrats, by="variable")
dat2 <- merge(dat2, republicans, by="variable")

# Create measure of partisan agreement ####

dat2$bipartisan <- 0
dat2$bipartisan[dat2$dem_support>0 & dat2$rep_support>0] <- 1
dat2$bipartisan[dat2$dem_support<0 & dat2$rep_support<0] <- 1

# Create measure of small loading ####
dat2$loads <- 0
dat2$loads[dat2$factor_score>.3 | dat2$factor_score< -.3] <- 1

# land use
dat2$landuse <- 0
dat2$landuse <- 0

dat2 <- dat2 %>% 
  mutate(landuse = if_else(variable %in% c("aesthetic_impact", "landuse_limits","affordable_housing","nimby","rent_controls"), 1, 0),
         redist = if_else(variable %in% c("affordable_housing","rent_controls","pre_education","public_transit","internet_access","cut_socialservices","healthcare","cut_domestic_spend"), 1, 0),
         taxes = if_else(variable %in% c("cut_raise_taxes","deficit_taxes","business_taxbreaks_1","business_taxbreaks_2","business_taxbreaks_3"), 1, 0),
         local = if_else(variable %in% c("affordable_housing",
                                         "rent_controls",
                                         "pre_education",
                                         "public_transit",
                                         "samesex_benefits",
                                         "landuse_limits",
                                         "aesthetic_impact",
                                         "business_taxbreaks_1",
                                         "business_taxbreaks_2",
                                         "business_taxbreaks_3",
                                         "increased_parking",
                                         "require_recycling",
                                         "increase_localpolice",
                                         "employee_pension",
                                         "internet_access",
                                         "nimby",
                                         "cut_socialservices",
                                         "deficit_taxes"),1,0),
         abs_fs = abs(factor_score))


model1 <- summary(lm(abs_fs ~ landuse + redist + taxes + local, data=dat2))
summary(lm(abs_fs ~ landuse + redist + taxes , data=subset(dat2, local==1)))

model2 <- summary(lm(abs_fs ~ landuse + redist + taxes + local + bipartisan, data=dat2))
summary(lm(abs_fs ~ landuse + redist + taxes + local + bipartisan + pct_ambivalent, data=dat2))

library(sjPlot)

tab_model(model1,model2,
          show.ci=FALSE, show.se=TRUE, show.p=F, digits.p=2,
          file = "tablea3.html")

sink()
